package com.hqq.service.impl;

import com.hqq.dao.WoocCardDao;
import com.hqq.dao.WoocCardDetailDao;
import com.hqq.dao.WoocTypeDao;
import com.hqq.dao.WoocUserDao;
import com.hqq.domain.WoocCard;
import com.hqq.domain.WoocCardDetail;
import com.hqq.domain.WoocType;
import com.hqq.domain.WoocUser;
import com.hqq.enums.SortTypeEnum;
import com.hqq.service.WoocCardService;
import com.hqq.vo.CardDetailVO;
import com.hqq.vo.CardRemarkVO;
import com.hqq.vo.CardVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * WoocCardServiceImpl
 * Created by heqianqian on 2018/3/25.
 */
@Service
public class WoocCardServiceImpl implements WoocCardService {
    private static final Logger logger = LoggerFactory.getLogger(WoocCardServiceImpl.class);

    @Autowired
    private WoocCardDao woocCardDao;
    @Autowired
    private WoocCardDetailDao woocCardDetailDao;
    @Autowired
    private WoocUserDao woocUserDao;
    @Autowired
    private WoocTypeDao woocTypeDao;

    /**
     * 查询帖子列表
     *
     * @param sortType 排序类型
     * @param typeId   资源类型
     * @return
     */
    @Override
    public List<CardVO> listCards(Long sortType, Long typeId) {
        List<CardVO> cardVOS = new ArrayList<>();
        //参数校验
        if (sortType == null || null == SortTypeEnum.enumValueOf(sortType)) {
            sortType = SortTypeEnum.NEWEST.toCode();
        }
        //查询发帖信息
        List<WoocCard> woocCards = woocCardDao.listWoocCard(sortType, typeId);
        //封装对象
        for (WoocCard woocCard : woocCards) {//TODO 加缓存
            cardVOS.add(getCardVO(woocCard.getCardId()));
        }
        logger.debug("===WoocCardServiceImpl listCards===cardVOS:{}", cardVOS);
        return cardVOS;
    }

    /**
     * 查询发帖详细信息
     *
     * @param cardId
     * @return
     */
    @Override
    public CardDetailVO getCardDetail(Long cardId) {
        Objects.requireNonNull(cardId, "发帖id不可为空");

        CardVO cardVO = getCardVO(cardId);
        List<CardRemarkVO> remarkList = getRemarkList(cardId);

        return CardDetailVO.builder()
                .cardVO(cardVO)
                .remarkVOList(remarkList)
                .build();
    }

    /**
     * 发表评论
     *
     * @param userId
     * @param cardId
     * @param remarkContent
     */
    @Override
    public CardRemarkVO publishRemark(Long userId, Long cardId, String remarkContent) {
        Objects.requireNonNull(userId, "用户id不可为空");
        Objects.requireNonNull(cardId, "发帖id不可为空");
        Objects.requireNonNull(remarkContent, "发帖内容不可为空");

        WoocCardDetail woocCardDetail = WoocCardDetail.builder()
                .cardDetailContent(remarkContent)
                .cardDetailUserId(userId)
                .cardId(cardId)
                .cardDetailCreateTime(new Date())
                .build();
        woocCardDetailDao.saveWoocCard(woocCardDetail);
        WoocUser user = woocUserDao.findWoocUserById(woocCardDetail.getCardDetailUserId());

        //更新回帖数
        woocCardDao.updateRemarkCount();

        return CardRemarkVO.builder()
                .cardDetailId(woocCardDetail.getCardDetailId())
                .cardDetailContent(woocCardDetail.getCardDetailContent())
                .cardDetailUserId(userId)
                .cardDetailUserName(user.getUserName())
                .cardDetailUserProfile(user.getUserProfile())
                .cardDetailCreateTime(woocCardDetail.getCardDetailCreateTime())
                .build();
    }

    /**
     * 发布帖子
     *
     * @param userId
     * @param cardTitle
     * @param cardContent
     * @param cardTypeId
     * @return
     */
    @Override
    public WoocCard publishCard(Long userId, String cardTitle, String cardContent, Long cardTypeId) {
        Objects.requireNonNull(userId, "用户id不可为空");
        Objects.requireNonNull(cardTitle, "发帖标题不可为空");
        Objects.requireNonNull(cardContent, "发帖内容不可为空");
        Objects.requireNonNull(cardTypeId, "发帖类型不可为空");

        WoocCard woocCard = WoocCard.builder()
                .cardTitle(cardTitle)
                .cardContent(cardContent)
                .cardUserId(userId)
                .cardCreateTime(new Date())
                .cardTypeId(cardTypeId)
                .cardRemarkCount(0L)
                .build();

        woocCardDao.insertWoocCard(woocCard);
        return woocCard;
    }

    /**
     * 删除帖子
     *
     * @param cardId
     */
    @Override
    public void deleteCard(Long cardId) {
        woocCardDao.deleteById(cardId);
        woocCardDetailDao.deleteByCardId(cardId);
    }

    @Override
    public WoocCard addCard(String cardTitle, String cardContent, Long typeId, Long userId) {
        WoocCard woocCard = WoocCard.builder()
                .cardTitle(cardTitle)
                .cardContent(cardContent)
                .cardTypeId(typeId)
                .cardUserId(userId)
                .cardCreateTime(new Date())
                .cardRemarkCount(0L)
                .build();
        woocCardDao.insertWoocCard(woocCard);
        return woocCard;
    }

    /**
     * 封装CardVO对象
     *
     * @param cardId
     * @return
     */
    private CardVO getCardVO(Long cardId) {
        WoocCard woocCard = woocCardDao.findWoocCardById(cardId);
        WoocUser woocUser = woocUserDao.findWoocUserById(woocCard.getCardUserId());
        WoocType woocType = woocTypeDao.findWoocTypeById(woocCard.getCardTypeId());
        return CardVO.builder()
                .cardId(woocCard.getCardId())
                .cardTitle(woocCard.getCardTitle())
                .cardContent(woocCard.getCardContent())
                .cardCreateTime(woocCard.getCardCreateTime())
                .cardTypeId(woocType.getTypeId())
                .cardTypeName(woocType.getTypeName())
                .cardRemarkCount(woocCard.getCardRemarkCount())
                .cardUserId(woocUser.getUserId())
                .cardUserName(woocUser.getUserName())
                .cardUserProfile(woocUser.getUserProfile())
                .build();
    }

    /**
     * 获取评论列表
     *
     * @param cardId
     * @return
     */
    private List<CardRemarkVO> getRemarkList(Long cardId) {
        List<CardRemarkVO> remarkVOS = new ArrayList<>();
        List<WoocCardDetail> details = woocCardDetailDao.findByCardId(cardId);
        //封装VO对象
        for (WoocCardDetail detail : details) {
            WoocUser user = woocUserDao.findWoocUserById(detail.getCardDetailUserId());
            CardRemarkVO remarkVO = CardRemarkVO.builder()
                    .cardDetailId(detail.getCardDetailId())
                    .cardDetailContent(detail.getCardDetailContent())
                    .cardDetailCreateTime(detail.getCardDetailCreateTime())
                    .cardDetailUserId(user.getUserId())
                    .cardDetailUserName(user.getUserName())
                    .cardDetailUserProfile(user.getUserProfile())
                    .build();
            remarkVOS.add(remarkVO);
        }
        return remarkVOS;
    }
}
